package br.com.sig.cadastro.recursoshumanos.presentation.action;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.jboss.seam.international.status.builder.BundleKey;

import br.com.sig.business.exception.BaseException;
import br.com.sig.cadastro.recursoshumanos.business.model.bo.DepartamentoBO;
import br.com.sig.cadastro.recursoshumanos.business.model.entity.Departamento;
import br.com.sig.presentation.action.BaseListAction;
import br.com.sig.security.BaseIdentity;
import br.com.sig.security.Identity;

@Named
@ViewScoped
public class DepartamentoList extends BaseListAction<Departamento> {

	private static final long serialVersionUID = -2433856030223629541L;
	
	@Inject
	@Identity
	private BaseIdentity baseIdentity;	
	
	@Inject
	private DepartamentoBO departamentoBO;

	@PostConstruct
	public void init() {
		setService(departamentoBO);
		setSortField("nome");
	}
	
	@Override
	public List<Departamento> getResultList() {
		
		if (this.resultList == null) {
			super.getResultList();
			
			if (!baseIdentity.isSystemAdministrator()) {
				Departamento departamento = new Departamento();
				departamento.setNome("Consultoria");
				
				try {
					departamento = departamentoBO.findSingleResult(departamento);
				} catch (BaseException e) {
					e.printStackTrace();
					messages.error(new BundleKey("messages", "error.find"));
				}
				
				resultList.remove(departamento);
			}
		}
		
		return resultList; 
	}	
}